package genericdemo;

//写一个泛型类，类中有个方法，求数组最大值
class Alg<T extends Comparable<T>> {

    public T findMax(T[] array) {
        T max = array[0];
        for (int i = 0; i < array.length; i++) {
            if (max.compareTo(array[i]) < 0) {
                max = array[i];
            }
        }
        return max;
    }

}

class Alg2 {
    /**
     * 静态的泛型方法
     * @param array
     * @param <T>
     * @return
     */
    public static<T extends Comparable<T>> T findMax(T[] array) {
        T max = array[0];
        for (int i = 0; i < array.length; i++) {
            if (max.compareTo(array[i]) < 0) {
                max = array[i];
            }
        }
        return max;
    }
}

public class TestDemo2 {

    public static void main(String[] args) {
        Integer[] array = {1, 2, 3, 4, 5};
        Integer max = Alg2.findMax(array);
        System.out.println(max);
    }

    public static void main1(String[] args) {
        Alg<Integer> alg = new Alg<>();
        Integer[] array = {1, 2, 3, 4, 5};
        Integer max = alg.findMax(array);
        System.out.println(max);
    }

}
